import { wxRequest } from '../../../utils/server.js';
import { terminalList } from '../../../utils/common.js';


const _wxRequest = new wxRequest();

const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    address: '',
    typeindex:0,
    terminalindex: 0,
    terminal: 1,
    devTypeId:0,
    devTypeName:'',
    devTypeIndex:0,
    adTypeList:[],
    sbTypeList:[],
    terminalList 
  },
  
  onLoad: function (options){
    this.uploadImgs = this.selectComponent("#uploadImgs");
    
    //编辑
    var wxitem = wx.getStorageSync('adpicItem')
    if (wxitem) {
       //初始化终端 
      let terminalindex='' 
      terminalList.map((item,index)=>{
        if (item.id == wxitem.terminal) {
          terminalindex = index; 
        }
      })

      this.setData({
        cententDetail: wxitem,
        typeId: wxitem.typeId,
        terminal: wxitem.terminal,
        terminalindex,
        
      })
      var imgArray = [{
        imgPath: wxitem.path
      }]

      this.uploadImgs.setData({
        datalist: imgArray,
        originImgs: imgArray
      })
    }
    this.getAdType()
  }, 

  onUnload: function () { 
    wx.removeStorageSync('adpicItem')
  },

  onHide:function(){
    wx.removeStorageSync('adpicItem')
  },

  onShow:function(){
    //this.getAdType()
  },

  //选择商品图片
  chooseGoodsImg: function (e) {
    this.setData({
      imgs: this.uploadImgs.data.imgs
    })
  },

  deleteGoodsImg: function (e) {
    this.setData({
      imgs: this.uploadImgs.data.imgs
    })
  },

  deloriginGoodsImg: function () {
    this.setData({
      originImgs: this.uploadImgs.data.originImgs
    })
  },
 

  formSubmit: function (e) {
    var formdata = e.detail.value; 
    formdata.typeId = this.data.typeId
    formdata.terminal = this.data.terminal
    formdata.path = this.uploadImgs.data.datalist[0].imgPath
    formdata.devTypeId = this.data.devTypeId
    formdata.devTypeName = this.data.devTypeName

    if (!formdata.typeId && formdata.typeId == '') {
      wx.showToast({
        icon: 'none',
        title: '请选择广告类型',
      })
      return
    }

    if (!formdata.name && formdata.name == '') {
      wx.showToast({
        icon: 'none',
        title: '名称不能为空',
      })
      return 
    }

    if (!formdata.path && formdata.path ==''){
      wx.showToast({
        icon: 'none',
        title: '图片不能为空',
      })
      return 
    }
    if (!formdata.devTypeId && formdata.devTypeId ==''){
      wx.showToast({
        icon: 'none',
        title: '设备类型不能为空',
      })
      return 
    }
    console.log('formdata:', formdata) 
    this.formRequest(formdata)
  },

  //请求
  formRequest: function (formdata){
    var url = '/sys/protected/comm/beanCrud/cms.AdPictureVO/create';
    var title = "发布成功"

    //修改
    if (this.data.cententDetail && this.data.cententDetail.id) {
      url = "/sys/protected/comm/beanCrud/cms.AdPictureVO/update";
      formdata.id = this.data.cententDetail.id;
      title = "修改成功"
    }
    
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/publish/adpicture/list',
        })
      }, 1500)
    })
  },


  goback: function () {
    wx.navigateBack({})
  },

  //选择类型
  typePickerChange:function(e){
    var typeindex = e.detail.value;
    var terminalList = this.data.terminalList
    var typeId='';

    terminalList.map((item,index)=>{
      if (index == typeindex){
        typeId=item.id
      }
    })

    this.setData({
      typeindex,
      typeId
    })
  },

  sbTypePickerChange:function(e){
    var devTypeIndex = e.detail.value;
    var sbTypeList = this.data.sbTypeList
    var devTypeId='';
    var devTypeName= ''


    sbTypeList.map((item,index)=>{
      if (index == devTypeIndex){
        devTypeId=item.id
        devTypeName = item.typeName
      }
    })
    this.setData({
      devTypeId,
      devTypeName,
      devTypeIndex
    })
  },


  //选择终端
  terminalChange: function (e) {
    var terminalindex = e.detail.value;
    var terminalList = this.data.terminalList
    var terminal = '';

    terminalList.map((item, index) => {
      if (index == terminalindex) {
        terminal = item.id
      }
    })

    this.setData({
      terminalindex,
      terminal
    })
  },

  //获取类型
  getAdType:function(){
    //请求
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/comm/beanCrud/cms.AdTypeVO/find',
      method: 'POST', 
      data:{
        pageSize:50
      }
    }, (res) => {
      console.log(res)
      var list = res.list

      //编辑情况下
      var wxitem = wx.getStorageSync('adpicItem')
      if (wxitem){
        list.map((item,index)=>{
          if (item.id ==wxitem.typeId){
            this.setData({
              typeindex:index
            })
          }
        })
      }else{
        //初始化，给默认值
        this.setData({
          typeId: list[0].id
        })
      }
      this.setData({
        adTypeList: list
      })
    })

    //设备类型列表
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/queryTypeList',
      method: 'POST', 
      data:{}
    }, (res) => {
      this.setData({
        sbTypeList: res.list
      })

      //编辑情况下
      var wxitem = wx.getStorageSync('adpicItem')
      if (wxitem){
        res.list.map((item,index)=>{
          if (item.id ==wxitem.devTypeId){
            console.log('wxitem-------------',index)

            this.setData({
              devTypeId: item.id,
              devTypeName: item.typeName,
              devTypeIndex:index
            })
          }
        })
      }else{
        //初始化，给默认值
        res.list.length > 0 && this.setData({
          devTypeId: res.list[0].id,
          devTypeName: res.list[0].typeName,
        })
      }

    })
  },

 



})